package com.dz.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.dz.constant.MessageConstant;
import com.dz.constant.StatusConstant;
import com.dz.dto.CategoryDTO;
import com.dz.dto.CategoryPageQueryDTO;
import com.dz.entity.Category;
import com.dz.entity.Dish;
import com.dz.entity.SetMeal;
import com.dz.enumeration.AppHttpCodeEnum;
import com.dz.exception.SystemException;
import com.dz.mapper.CategoryMapper;
import com.dz.mapper.DishMapper;
import com.dz.mapper.SetMealMapper;
import com.dz.result.Result;
import com.dz.service.CategoryService;
import com.dz.utils.BeanCopyUtils;
import com.dz.vo.PageVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author 鄧浩奇
 * @classname CategoryServiceImpl
 * @description
 * @since 2023/12/16 21:37
 **/

@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private DishMapper dishMapper;

    @Resource
    private SetMealMapper setMealMapper;

    /*
     * 分类分页查询
     * */
    @Override
    public Result<PageVo> page(CategoryPageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.page(pageQueryDTO);
        PageVo pageVo = new PageVo(page.getResult(), page.getTotal());
        return Result.success(pageVo);
    }


    /*
     * 新增分类
     * */
    @Override
    public Result<String> addCategory(CategoryDTO categoryDTO) {
        Category byName = categoryMapper.selectByName(categoryDTO.getName());
        if (Objects.nonNull(byName)) {
            return Result.error(AppHttpCodeEnum.CATEGORY_EXIST.getMsg());
        }
        Category category = BeanCopyUtils.copyBean(categoryDTO, Category.class);
        if (categoryMapper.insertCategory(category).equals(StatusConstant.UPDATE_ERROR))
            return Result.error(AppHttpCodeEnum.INSERT_ERROR.getMsg());
        return Result.success();
    }


    /*
     * 修改分类
     * */
    @Override
    public Result<String> update(CategoryDTO categoryDTO) {
        Category category = BeanCopyUtils.copyBean(categoryDTO, Category.class);
        if (categoryMapper.update(category).equals(StatusConstant.UPDATE_ERROR))
            return Result.error(AppHttpCodeEnum.UPDATE_ERROR.getMsg());
        return Result.success();
    }


    /*
     * 启用、禁用分类
     * */
    @Override
    public Result<String> changeStatus(Long id, Integer status) {
        if (Objects.isNull(id))
            return Result.error(AppHttpCodeEnum.CATEGORY_ID_IS_NULL.getMsg());
        if (Objects.isNull(status))
            return Result.error(AppHttpCodeEnum.STATUS_IS_NULL.getMsg());
        if (!status.equals(StatusConstant.DISABLE) && !status.equals(StatusConstant.ENABLE))
            return Result.error(AppHttpCodeEnum.STATUS_ERROR.getMsg());
        if (StatusConstant.UPDATE_ERROR.equals(categoryMapper.changeStatus(id, status)))
            return Result.error(AppHttpCodeEnum.UPDATE_ERROR.getMsg());
        return Result.success();
    }


    /*
     * 删除分类
     * */
    @Override
    public Result<String> deleteCategory(Long id) {
        if (Objects.isNull(id))
            return Result.error(AppHttpCodeEnum.CATEGORY_ID_IS_NULL.getMsg());

        // 如果分类下面有 菜品 或 套餐 不能删除
        List<Dish> dishes = dishMapper.selectByCategoryId(id);
        List<SetMeal> setMeals = setMealMapper.selectByCategoryId(id);

        if (Objects.isNull(dishes) || Objects.isNull(setMeals))
            throw new SystemException(AppHttpCodeEnum.SYSTEM_ERROR);

        if (!dishes.isEmpty()) return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        if (!setMeals.isEmpty()) return Result.error(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);

        if (StatusConstant.DELETE_ERROR.equals(categoryMapper.removeCategory(id)))
            return Result.error(AppHttpCodeEnum.UPDATE_ERROR.getMsg());
        return Result.success();
    }


    /*
    * 根据分类id查询分类列表
    * */
    @Override
    public List<Category> getListByType(Integer type) {
        return categoryMapper.selectByType(type);
    }
}
